# -*- coding:utf-8 -*-
# @time:2020-8-25 9:52
# POWERD BY:ECHO
import time
#from random import random

import cv2
from selenium import webdriver
from selenium.webdriver import ActionChains

from getimg import get_img,FindPic

driver = webdriver.Chrome("D:\slide\chromedriver.exe")
driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://h.cn/")

driver.find_element_by_xpath('//*[@id="__layout"]/div/div[1]/div/div[1]/div[1]/ul/li[5]/a').click()

driver.find_element_by_xpath('//*[@id="__layout"]/div/div[2]/div/div[2]/section[1]/div[2]/div[2]/form/div[1]/div/div[1]/input').send_keys('18617099614')
driver.find_element_by_xpath('//*[@id="__layout"]/div/div[2]/div/div[2]/section[1]/div[2]/div[2]/form/div[2]/div/div/input').send_keys('hd123456')
driver.find_element_by_xpath('//*[@id="__layout"]/div/div[2]/div/div[2]/section[1]/div[2]/div[2]/form/div[4]/div/button').click()


time.sleep(5)

ele1 = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/div/div/div[1]/div/div[1]/img[1]')
ele2 = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/div/div/div[1]/div/div[1]/img[2]')
get_img(ele1.get_attribute("src"),'./lib/target.png')
get_img(ele2.get_attribute("src"),'./lib/template.png')

x = FindPic()
print(x)

w1 = ele1.size['width']
print(w1)
img = cv2.imread('./lib/target.png')
w2 = img.shape[1]
print(w2)

x = int(x*w1/w2)
#y = random.randint(1,10)
print(x)
#滑动
action = ActionChains(driver)
action.click_and_hold(ele2)#按住元素
action.move_by_offset(x,0)
# action.move_by_offset(x-y,0).perform() #滑动x和y
# time.sleep(1)
# action.move_by_offset(y,0)
action.release().perform() #释放鼠标

time.sleep(5)

driver.find_element_by_xpath('//*[@id="__layout"]/div/div[1]/div/div[1]/div[1]/ul/li[5]/a[2]').click()
driver.quit()

